/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package overlaynetwork;

import java.util.concurrent.*;
import protocol.*;
import java.util.*;
import java.net.*;

public class QPacketsController extends Thread{

    private OverlayNetwork overlay;

    public QPacketsController(OverlayNetwork overlay){
     this.overlay = overlay;

    }

    public void run(){
        
        while(true){
        try{

        Packet p= overlay.getQPackets().take();
     
        if(!overlay.getCachePackets().containsKey(p.getGUID())){
        overlay.getCachePackets().put(p.getGUID(), p);

        switch(p.getMessageType()){
            case MyProtocol.PING:{
                
                if(p.getTtl()!=0){
                   p.incrementHops();
                  

                   ((Vector<InetSocketAddress>)p.getMessage()).add(overlay.getInetSocketAddress());
                   
                   System.out.println(p);
                   overlay.connect().sendPacket(p);
                }

            }
            break;

        }
        }
        } catch(Exception e){
            System.err.println("errore in qPacketsController");
            e.printStackTrace();
        }
    }
    }

}
